﻿using System.ComponentModel;
using System.Net;
using System.Net.Mail;
using System.Text;
using Super.Infrastructure;

namespace Super.DomainService.System
{
    public class MailSender
   {
       private readonly DbContextAdapter _adp;
      // private readonly SettingService CacheService;

       public MailSender(DbContextAdapter adapter)
       {
           _adp = adapter;
           //CacheService=new SettingService(_adp);
           
       }

       public  void Send(string server, string sender, string recipient, string subject, string body, bool isBodyHtml, Encoding encoding, bool isAuthentication, params string[] files)
       {
           var smtpClient = new SmtpClient(server);
           var message = new MailMessage(sender, recipient);
           message.IsBodyHtml = isBodyHtml;

           message.SubjectEncoding = encoding;
           message.BodyEncoding = encoding;

           message.Subject = subject;
           message.Body = body;


           message.Attachments.Clear();
           if (files != null && files.Length != 0)
           {
               for (int i = 0; i < files.Length; ++i)
               {
                   Attachment attach = new Attachment(files[i]);
                   message.Attachments.Add(attach);
               }
           }

           if (isAuthentication == true)
           {
               smtpClient.Credentials = new NetworkCredential(CacheService.GetSetting().SmtpUserName,
                   CacheService.GetSetting().SmtpPassword);
           }

           // smtpClient.Send(message);

           smtpClient.SendCompleted += new SendCompletedEventHandler(SmtpClientSendCompleted);
           //object mailSendState = message;
           //smtpClient.SendAsync(message, mailSendState);
           smtpClient.SendAsync(message, "ok");


       }

       public  void SmtpClientSendCompleted(object sender, AsyncCompletedEventArgs e)
       {
           bool mailSent = true;
           string state = (string)e.UserState;

           if (e.Cancelled)
           {
               mailSent = false;
           }
           if (e.Error != null)
           {
               mailSent = false;
           }


           ////System.Net.Mail.MailMessage mail = (System.Net.Mail.MailMessage)e.UserState;
           ////string subject = mail.Subject;
           ////string content = mail.Body;

           //if (e.Cancelled)
           //{
           //    mailSent = false;
           //}
           //if (e.Error != null)
           //{
           //    mailSent = false;
           //}
           //if (!mailSent)
           //{
           //    //string file = "Send Mail/Send Mail test asnc Error " + System.DateTime.Now.ToString("yyyy-MM-dd") + ".txt";

           //    //StringBuilder sb = new StringBuilder();
           //    //sb.Append("Send Failed! ");
           //    //sb.Append("Subject :" + subject + " ");
           //    //sb.Append("Content :" + content);
           //    //sb.Append("Exception :" + e.Error.ToString());
           //    //WriteLog.Write(sb.ToString(), file);
           //}
           //else
           //{
           //    //string file = "Send Mail/Send Mail test asnc Error " + System.DateTime.Now.ToString("yyyy-MM-dd") + ".txt";

           //    //StringBuilder sb = new StringBuilder();
           //    //sb.Append("Send Sucess! ");
           //    //sb.Append("Subject :" + subject + " ");
           //    //sb.Append("Content :" + content);
           //    //WriteLog.Write(sb.ToString(), file);
           //}

       }

        public void Send(string recipient, string subject, string body)
       {
           if (string.IsNullOrEmpty(recipient))
               return;

           Send(CacheService.GetSetting().SmtpServer, CacheService.GetSetting().SmtpEmail, recipient, subject, body, true, Encoding.UTF8, true, null);
       }

       public  void Send(string recipient, string sender, string subject, string body)
       {
           if (string.IsNullOrEmpty(recipient))
               return;

           Send(CacheService.GetSetting().SmtpServer, sender, recipient, subject, body, true, Encoding.UTF8, true, null);
       }

   }
}
